﻿using System;
using Sunny.Core.DefinedAttribute;
using Sunny.Core.DefinedData;

namespace Sunny.Core.Domain
{
    public class ConstFactory
    {
        public static SList<SListItem> ConvertToListItemCollection<T>()
        {
            var type = typeof(T);

            var list = new SList<SListItem>();
            
            var fields = type.GetFields();

            for (int i = 1; i < fields.Length; i++)
            {
                var value = fields[i].Name;
                if (value.ToLower() != "none")
                {
                    var attributies = fields[i].GetCustomAttributes(typeof(ConstAttribute), false);

                    if(attributies.Length == 0)
                    {
                        list.Add(new SListItem
                        {
                            Text = value,
                            Value = value
                        });
                    }
                    else
                    {
                        list.Add(new SListItem
                        {
                            Text = (attributies[0] as ConstAttribute).ViewString,
                            Value = (attributies[0] as ConstAttribute).ViewString
                        });
                    }
                }
            }

            return list;
        }

        public static T ConvertToEnum<T>(string value)
        {
            // Get all feilds
            var fields = typeof(T).GetFields();
            for (int i = 1; i < fields.Length; i++)
            {
                // Get attributies
                var attributies = fields[i].GetCustomAttributes(typeof(ConstAttribute), false);

                if (value.ToLower() == fields[i].Name.ToLower() || (attributies.Length > 0 && (attributies[0] as ConstAttribute).ViewString == value))
                {
                    return (T)Enum.Parse(typeof(T), fields[i].Name);
                }
            }

            return (T)Enum.Parse(typeof(T), string.Empty);
        }

        public static string ConvertToString(object value)
        {
            object[] attributies = null;

            // Get all feilds
            var fields = value.GetType().GetFields();
            for (int i = 1; i < fields.Length; i++)
            {
                if(value.ToString().ToLower() == fields[i].Name.ToLower())
                {
                    // Get attributies
                    attributies = fields[i].GetCustomAttributes(typeof (ConstAttribute), false);
                    break;
                }
            }

            // Get view string
            if(attributies == null || attributies.Length == 0)
            {
                return value.ToString();  
            }
            else
            {
                return (attributies[0] as ConstAttribute).ViewString;
            }
        }

        public static bool IsEqual(object value1, string value2)
        {
            return ConvertToString(value1) == value2;
        }
    }
}